package com.yu.xcx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.xcx.dto.Result;
import com.yu.xcx.entity.Shop;
import com.yu.xcx.entity.ShopFollow;
import com.yu.xcx.mapper.ShopFollowMapper;
import com.yu.xcx.mapper.ShopMapper;
import com.yu.xcx.service.IShopFollowService;
import com.yu.xcx.service.IShopService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ShopFollowServiceImpl extends ServiceImpl<ShopFollowMapper, ShopFollow> implements IShopFollowService {

    @Resource
    private ShopFollowMapper shopFollowMapper;
    @Resource
    private IShopService shopService;

    @Override
    public Result follow(ShopFollow shopFollow, boolean flag) {

        if (flag){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id",shopFollow.getUserId());
            queryWrapper.eq("shop_id", shopFollow.getShopId());
            boolean result = remove(queryWrapper);
            if (result){
                return Result.ok("取消收藏成功");
            }else {
                return Result.ok("取消收藏失败");
            }
        }
        boolean result = save(shopFollow);
        if (result){
            return Result.ok("添加收藏成功");
        }else{
            return Result.ok("添加收藏失败");
        }
    }

    @Override
    public Result queryFollowShop(Integer userId, Integer pagenum) {
        List<Integer> shopList = shopFollowMapper.getShopByUser(userId);
        return shopService.getShopsById(shopList, pagenum);
    }

    @Override
    public Boolean isFollow(ShopFollow shopFollow) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",shopFollow.getUserId());
        queryWrapper.eq("shop_id",shopFollow.getShopId());
        Integer count = count(queryWrapper);
        if (count == 1){
            return true;
        }
        return false;
    }
}
